package libs;


import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;
import javax.swing.JOptionPane;
import com.thoughtworks.xstream.XStream;
import types.TMLPNode;
import types.TNNTopology;


public class GenericLib {

	ArrayList<TMLPNode> discriminantNodes=new ArrayList<TMLPNode>();
	
	public Vector yaz2DArrayF(float[][] param,String fileName,int dimensionReductionSize){
		discriminantNodes.clear();
		println("first layer dimensions:"+param.length+";"+param[0].length);
		float[] temp=new float[param[0].length];
		for (int i = 0; i < param[0].length; i++) {
			TMLPNode node=new TMLPNode();
			node.pos=i;
			for (int j = 0; j < param.length; j++) {
				float a=Math.abs(param[j][i]);
				temp[i]+=a;
				//k++;
				node.totalWeight+=a;
			}
			discriminantNodes.add(node);
		}
		
		sortArrayList(1,discriminantNodes);
		
		ArrayList<TMLPNode> tempList=new ArrayList<TMLPNode>();
		
		if (param[0].length<=dimensionReductionSize) {
			dimensionReductionSize=param[0].length;
		}
		for (int i = 0; i < dimensionReductionSize; i++) {
			tempList.add(discriminantNodes.get(i));
		}
		XStream xx = new XStream();
		String str1 = xx.toXML(tempList);
		Utils.writeToFile("C:\\DataSet_yeni\\DS\\DiscrPixels\\"+fileName+".xml", str1);
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public static void sortArrayList(int n,ArrayList<TMLPNode> ds) {
		try {
			//buyukkten kucugee siralar
			if (n>0) {
				Collections.sort(ds,new Comparator(){
					public int compare(Object a,Object b){
						if (((TMLPNode)a).totalWeight<((TMLPNode)b).totalWeight) {
							return 1;
						}else{
							return 0;
						}
					}
				});
			}else{
				Collections.sort(ds,new Comparator(){
					public int compare(Object a,Object b){
						if (((TMLPNode)a).totalWeight>((TMLPNode)b).totalWeight) {
							return 1;
						}else{
							return 0;
						}
					}
				});
			}

		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	

	public void showMessage(String str) {
		JOptionPane.showMessageDialog(null, str);
	}

	private void println(String msg){
		System.out.println(msg);
	}

	
	public void toFile(TNNTopology nns, String fileName,String driver) {
		XStream xx = new XStream();
		String str1 = xx.toXML(nns);
		Utils.writeToFile(driver+"\\DataSet_yeni\\DS\\DiscrPixels\\"+fileName+".xml", str1);
	}
	
}
